<!DOCTYPE html>
<html>
<head>
    <!-- 页面meta -->
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>传智健康</title>
    <meta name="description" content="传智健康">
    <meta name="keywords" content="传智健康">
    <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
    <!-- 引入样式 -->
    <link rel="stylesheet" href="../plugins/elementui/index.css">
    <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
    <link rel="stylesheet" href="../css/style.css">
    <!-- 引入组件库 -->
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <style>
        .datatable {
            position: relative;
            box-sizing: border-box;
            -webkit-box-flex: 1;
            width: 100%;
            max-width: 100%;
            font-size: 14px;
            color: rgb(96, 98, 102);
            overflow: hidden;
            flex: 1 1 0%;
        }

        .datatable td, .datatable th {
            padding: 12px 0;
            min-width: 0;
            -webkit-box-sizing: border-box;
            box-sizing: border-box;
            text-overflow: ellipsis;
            vertical-align: middle;
            position: relative;
            text-align: left;
        }
    </style>
</head>
<body class="hold-transition">
<div id="app">
    <div class="content-header">
        <h1>后台管理
            <small>菜单管理</small>
        </h1>
        <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>后台管理</el-breadcrumb-item>
            <el-breadcrumb-item>菜单管理</el-breadcrumb-item>
        </el-breadcrumb>
    </div>
    <div class="app-container">
        <div class="box">
            <div class="filter-container">
                <el-input placeholder="编码/菜单名称" v-model="pagination.queryString" style="width: 200px;"
                          class="filter-item" @keyup.enter.native="handleFilter"></el-input>
                <el-button @click="findPage()" class="dalfBut">查询</el-button>
                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
            </div>
            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                <el-table-column type="index" align="center" label="序号"></el-table-column>
                <el-table-column prop="name" label="菜单名称" align="center"></el-table-column>
                <el-table-column prop="linkUrl" label="访问路径" align="center"></el-table-column>
                <el-table-column prop="path" label="路由路径" align="center"></el-table-column>
                <el-table-column prop="priority" label="优先级" align="center"></el-table-column>
                <el-table-column prop="icon" label="图标" align="center"></el-table-column>
                <el-table-column label="操作" align="center">
                    <template slot-scope="scope">
                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination-container">
                <el-pagination
                        class="pagiantion"
                        @current-change="handleCurrentChange"
                        :current-page="pagination.currentPage"
                        :page-size="pagination.pageSize"
                        layout="total, prev, pager, next, jumper"
                        :total="pagination.total">
                </el-pagination>
            </div>
            <!-- 新增标签弹层 -->
            <div class="add-form">
                <el-dialog title="新增菜单" :visible.sync="dialogFormVisible">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>

                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="访问路径">
                                                <el-input v-model="formData.linkUrl" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="优先级">
                                                <el-input v-model="formData.priority" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>权限控制</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="checkitemIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible = false">取消</el-button>
                        <el-button type="primary" @click="handleAdd()">确定</el-button>
                    </div>
                </el-dialog>
            </div>


            <!-- 编辑标签弹层 -->
            <div class="add-form">
                <el-dialog title="编辑菜单" :visible.sync="dialogFormVisible4Edit">
                    <template>
                        <el-tabs v-model="activeName" type="card">
                            <el-tab-pane label="基本信息" name="first">
                                <el-form label-position="right" label-width="100px">
                                    <el-row>
                                        <el-col :span="12">
                                            <el-form-item label="名称">
                                                <el-input v-model="formData.name"/>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="访问路径">
                                                <el-input v-model="formData.linkUrl" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                    <el-row>
                                        <el-col :span="24">
                                            <el-form-item label="优先级">
                                                <el-input v-model="formData.priority" type="textarea"></el-input>
                                            </el-form-item>
                                        </el-col>
                                    </el-row>
                                </el-form>
                            </el-tab-pane>
                            <el-tab-pane label="角色信息" name="second">
                                <div class="checkScrol">
                                    <table class="datatable">
                                        <thead>
                                        <tr>
                                            <th>选择</th>
                                            <th>角色名称</th>
                                            <th>权限控制</th>
                                            <th>描述</th>
                                        </tr>
                                        </thead>
                                        <tbody>
                                        <tr v-for="c in tableData">
                                            <td>
                                                <input :id="c.id" v-model="checkitemIds" type="checkbox" :value="c.id">
                                            </td>
                                            <td><label :for="c.id">{{c.name}}</label></td>
                                            <td><label :for="c.id">{{c.keyword}}</label></td>
                                            <td><label :for="c.id">{{c.description}}</label></td>
                                        </tr>
                                        </tbody>
                                    </table>
                                </div>
                            </el-tab-pane>
                        </el-tabs>
                    </template>
                    <div slot="footer" class="dialog-footer">
                        <el-button @click="dialogFormVisible4Edit = false">取消</el-button>
                        <el-button type="primary" @click="handleEdit()">确定</el-button>
                    </div>
                </el-dialog>
            </div>
        </div>
    </div>
</div>
</body>

<script>
    var vue = new Vue({
        el: '#app',
        data: {
            activeName: 'first',//添加/编辑窗口Tab标签名称
            pagination: {//分页相关属性
                currentPage: 1,
                pageSize: 10,
                total: 100,
                queryString: null,
            },
            dataList: [],//列表数据
            formData: {},//表单数据
            tableData: [],//新增和编辑表单中对应的菜单列表数据
            checkitemIds: [],//新增和编辑表单中菜单对应的复选框，基于双向绑定可以进行回显和数据提交
            dialogFormVisible: false,//控制添加窗口显示/隐藏
            dialogFormVisible4Edit: false//控制编辑窗口显示/隐藏
        },

        //钩子函数，VUE对象初始化完成后，自动执行的方法
        created() {
            this.findPage();
        },


        methods: {


            //编辑的“确认键”
            handleEdit() {

                //发送ajax请求，存储编辑后的菜单信息
                axios.post("/menu/edit.do?roleIds=" + this.checkitemIds, this.formData).then((res) => {

                    //判断后台是否执行成功
                    if (res.data.flag) {

                        //执行成功，给出提示
                        this.$message({
                            message: res.data.message,
                            type: 'success'
                        });

                    }
                    else {

                        //执行失败，给出提示
                        this.$message.error(res.data.message);
                    }

                }).finally(() => {

                    //重新进行分页查询
                    this.findPage();

                });
                //关闭编辑窗口
                this.dialogFormVisible4Edit = false;
            },


            //添加的“确认键“”
            handleAdd() {

                //发送ajax请求，用于数据提交。因为要提交基本信息和检查项信息。
                //所以携带数据时，URL中可以有一个，第二个参数中可以有一个
                axios.post("/menu/add.do?roleIds=" + this.checkitemIds, this.formData).then((res) => {

                    //判断响应结果
                    if (res.data.flag) {

                        //执行成功，给出提示信息
                        this.$message({
                            type: "success",
                            message: res.data.message
                        });
                    }
                    else {
                        //执行失败，给出提示信息
                        this.$message.error(res.data.message);
                    }

                    //finnally在then后可以使用，表示回调是否成功，都会执行的方法。
                }).finally(() => {

                    //重新进行分页查询
                    this.findPage();

                });
                //关闭新增窗口
                this.dialogFormVisible = false;
            },


            //分页查询
            findPage() {

                //封装ajax请求所需要的数据
                var param = {
                    //当前页
                    currentPage: this.pagination.currentPage,
                    //每页显示的条数
                    pageSize: this.pagination.pageSize,
                    //查询条件
                    queryString: this.pagination.queryString
                };

                //发送ajax请求
                axios.post("/menu/findPage.do", param).then((res) => {

                    //把total进行赋值
                    this.pagination.total = res.data.total;

                    //把rows进行赋值
                    this.dataList = res.data.rows;
                });

            },


            // 重置表单
            resetForm() {
                //赋值为空即可
                this.formData = {};
                //初始化复选框
                this.checkitemIds = [];
                //设置默认打开窗口
                this.activeName = 'first';
            },


            // 弹出添加窗口
            handleCreate() {

                //打开添加窗口
                this.dialogFormVisible = true;
                //调用重置表单
                this.resetForm();


                //发送ajax请求，查询所有角色的信息
                //因为业务比较简单，不需要传递任何参数
                axios.get("/menu/findAll.do").then((res) => {

                    //判断执行的结果
                    if (res.data.flag) {
                        //后台执行成功,赋值给tableData
                        //把响应到的数据，
                        this.tableData = res.data.data;

                    }

                    else {
                        //后台执行失败，给出提示信息
                        this.$message.error(res.data.message);
                    }

                });

            },


            // 弹出编辑窗口
            handleUpdate(row) {

                //打开窗口
                this.dialogFormVisible4Edit = true;
                //设置默认打开窗口
                this.activeName = 'first';

                //第一次：发送ajax请求，用于菜单基本数据的回显
                axios.get("/menu/findByIdBack_J.do?id=" + row.id).then((res) => {

                    //判断后台执行的接过
                    if (res.data.flag) {
                        //执行成功，进行赋值
                        this.formData = res.data.data;
                    }
                });
                //第二次：发送ajax请求，显示出所有角色
                axios.get("/menu/findAll.do").then((res) => {

                    //判断执行的结果
                    if (res.data.flag) {
                        //后台执行成功,赋值给tableData
                        //把响应到的数据，
                        this.tableData = res.data.data;

                        //第三次：发送ajax请求，查询出菜单中包括哪几个角色，进行赋值回显
                        axios.get("/menu/findRoles.do?id=" + row.id).then((res) => {

                            //判断后台执行是否成功
                            if (res.data.flag) {

                                //执行成功，不需要提示了，否则体验不要。给复选框赋值，用于回显
                                this.checkitemIds = res.data.data;
                            }
                            else {
                                //执行失败，给出提示
                                this.$message.error(res.data.message);
                            }

                        });
                    }

                    else {
                        //后台执行失败，给出提示信息
                        this.$message.error(res.data.message);
                    }
                });

            },


            //切换页码。即当前页码改变时触发的查询方法
            handleCurrentChange(currentPage) {

                //设置最新的页码，把其赋值给模型对象pagination中的当前页码
                this.pagination.currentPage = currentPage;

                //调用分页查询的方法
                this.findPage();
            },


            // 删除
            handleDelete(row) {
                //为防止用户误操作，给用户弹出框提示信息。使用的弹出框时element的，为：$confirm()
                this.$confirm('此操作将永久删除该文件, 是否继续?', '警告', {

                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'

                }).then((res) => {//用户确定时

                    //发送ajax请求，提交ID数据
                    axios.get("/menu/deleteById.do?id=" + row.id).then((res) => {

                        //判断后台是否执行成功
                        if (res.data.flag) {

                            //执行成功，给出提示
                            this.$message({

                                type: "success",
                                message: res.data.message
                            });

                            //重新进行分页查询
                            this.findPage();
                        }

                        else {
                            //执行失败，给出提示
                            this.$message.error(res.data.message);

                        }
                    });

                }).catch(() => {//用户取消时

                    //用户取消时，给出提示
                    this.$message({
                        type: "info",
                        message: "操作已取消"
                    });

                });

            },
        }
    })

</script>
</html>
